package com.six.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.six.entity.News;
import com.six.entity.Product;
import com.ljw.util.DBManager;
import com.ljw.util.QueryDataResult;

/**
 * 产品展示页面 冯心语
 * 
 * @author DELL
 * @param <QueryDataResult>
 *
 */

public class ProductDaoImpl {

	public ArrayList<Product> getAllList() {

		String sql = "select * from product";
		System.out.println("ִ执行的" + sql);

		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Product> list = new ArrayList<Product>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Product e = new Product();
					e.setId(rs.getInt("Id"));
					e.setName(rs.getString("name"));
					e.setProtype(rs.getInt("protype"));
					e.setColor(rs.getString("color"));

					e.setPrice(rs.getDouble("price"));
					e.setBrand(rs.getString("brand"));
					e.setStyle(rs.getString("style"));
					e.setMaterial(rs.getString("material"));
					e.setZuzhuang(rs.getString("zuzhuang"));
					e.setDingzhi(rs.getString("dingzhi"));
					e.setPicurl(rs.getString("picurl"));
					e.setZhanshi(rs.getString("zhanshi"));

					list.add(e);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

	/**
	 * 6
	 * 
	 * @return
	 */
	public ArrayList<Product> getListLimit6() {

		String sql = "select * from product where zhanshi = '是' limit 6";
		System.out.println("执行的语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Product> list = new ArrayList<Product>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Product e = new Product();
					e.setId(rs.getInt(1));
					e.setName(rs.getString("name"));
					e.setProtype(rs.getInt("protype"));
					e.setColor(rs.getString("color"));
					e.setPicurl(rs.getString("picurl"));
					// ʡ
					list.add(e);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

	/**
	 * 通过新闻id获取新闻列表
	 * 
	 * @param typeId
	 * @return
	 */
	/*
	 * public ArrayList<News> getListByTypeId(int typeId) { String sql =
	 * "select * from news where typeId="+typeId+" ";
	 * System.out.println("执行的sql语句是"+sql); QueryDataResult execQuery =
	 * DBManager.executeQuery(sql); ArrayList<News> list = new ArrayList<News>(); if
	 * (execQuery != null) { ResultSet rs = execQuery.rs; try { while (rs.next()) {
	 * News e = new
	 * News(rs.getInt(1),rs.getString("title"),rs.getString("content"),rs.getInt(
	 * "typeId"),rs.getString("time"),rs.getString("showHome")); list.add(e); } }
	 * catch (SQLException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } finally { execQuery.close(); }
	 * 
	 * } }
	 */

	/**
	 * 通过id获取产品列表
	 * 
	 * @param protype
	 * @return
	 */
	public ArrayList<Product> getListByTypeId(int protype) {

		String sql = "select * from product where protype=" + protype + " ";
		System.out.println("执行的语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Product> list = new ArrayList<Product>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Product e = new Product();
					e.setId(rs.getInt(1));
					e.setName(rs.getString("name"));
					e.setProtype(rs.getInt("protype"));
					e.setColor(rs.getString("color"));
					e.setPicurl(rs.getString("picurl"));

					list.add(e);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

	/**
	 * 单查
	 * 
	 * @param id
	 * @return
	 */
	public Product getProById(int id) {
		String sql = "select * from product where id = " + id;
		QueryDataResult executeQuery = DBManager.executeQuery(sql);
		System.out.println("执行的sql语句" + sql);
		if (executeQuery != null) {
			ResultSet rs = executeQuery.rs;
			try {
				while (rs.next()) {
					Product p = new Product();
					p.setId(rs.getInt("id"));
					p.setName(rs.getString("name"));
					p.setProtype(rs.getInt("protype"));
					p.setColor(rs.getString("color"));
					p.setPrice(rs.getDouble("price"));
					p.setBrand(rs.getString("brand"));
					p.setStyle(rs.getString("style"));
					p.setMaterial(rs.getString("material"));
					p.setZuzhuang(rs.getString("zuzhuang"));
					p.setDingzhi(rs.getString("dingZhi"));
					p.setPicurl(rs.getString("picurl"));
					p.setZhanshi(rs.getString("zhanshi"));
					System.out.println(p);
					return p;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				executeQuery.close();
			}
		}
		return null;
	}

	public Product getAllList1() {

		String sql = "select * from product";
		System.out.println("ִ执行的" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Product e = new Product();
					e.setId(rs.getInt("Id"));
					e.setName(rs.getString("name"));
					e.setProtype(rs.getInt("protype"));
					e.setColor(rs.getString("color"));

					e.setPrice(rs.getDouble("price"));
					e.setBrand(rs.getString("brand"));
					e.setStyle(rs.getString("style"));
					e.setMaterial(rs.getString("material"));
					e.setZuzhuang(rs.getString("zuzhuang"));
					e.setDingzhi(rs.getString("dingzhi"));
					e.setPicurl(rs.getString("picurl"));
					e.setZhanshi(rs.getString("zhanshi"));
					return e;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		//System.out.println(e);
		return null;
	}

}
